package com.activenetwork.smlite.testcase.events.createEvents.basics;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.activenetwork.schema.testcase.events.createevents.basics.basics004.Basics004DataType;
import com.activenetwork.smlite.business.EventBusiness;
import com.activenetwork.smlite.page.event.EventDetailView.EventBasicSubview;
import com.activenetwork.smlite.snippet.EventsSnippet;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.jaxb.ScriptDataBindHelper;
import com.activenetwork.util.verification.gson.FailureAssertion;
import com.activenetwork.util.verification.gson.FailurePoint;

public class Basics004 extends BaseCase {
	static final Logger logger = LoggerFactory.getLogger(Basics004.class);
	private Basics004DataType data;
	
	@BeforeClass(alwaysRun = true)
	public void dataSetup() {	
		try{
			ScriptDataBindHelper helper = new ScriptDataBindHelper(Basics004.class);
			data = helper.bind(Basics004DataType.class);
		}catch(IOException e){
			throw new RuntimeException("Unable to bind the script data for " + this.getClass().getName());
		}
	}
	
	@Test
	public void Verify_add_Location_from_suggest_list_works_correctly(){
		
		loginBusiness.login();
		/*
		 * Try to add existing location
		 * if not exist, add an event with location, use that location
		 */
		EventBasicSubview eventBasicPage = EventsSnippet.gotoAddEventPage();
		
		try{
			eventBasicPage = eventBasicPage.selectEventLocationAJAX(data.getEvent().getBasic().getLocation());
			// logout
			loginBusiness.logout();
			return;
		}catch(RuntimeException e){
			logger.info("No existing location for selection, we need to create a one");
			eventBasicPage.events();
		}
		
		logger.info("Create a new event with location first.");
		EventBusiness eventBusiness = new EventBusiness();
		eventBusiness.addEvent(data.getEvent());
		
		logger.info("Go back to home view page");
		eventBasicPage = EventsSnippet.gotoAddEventPage();
		
		logger.info("Try create another event and try to select existing location created above.");
		try{
			eventBasicPage = eventBasicPage.selectEventLocationAJAX(data.getEvent().getBasic().getLocation());
		}catch(RuntimeException e){
			EnvContext.failurePointTL.get().add(
					new FailurePoint(Basics004.class.getName(), "Verify select existing location", "Unable to use existing location"));
		}
	
		// logout
		loginBusiness.logout();
		
		// Popup failures
		FailureAssertion.pop(Basics004.class);
	}

}
